package com.xkdx.guangguang.fragment.shop.search;

import java.util.HashMap;
import java.util.List;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.ShopActivity;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.shareclass.Shop;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class SearchShopFragment extends BaseFragment implements OnClickListener {
	View view;

	Button search_btn;
	ImageView shut;
	EditText search;
	ImageView searchPoint;

	String key;
	String Distance = "-1";
	List<Shop> list;
	ProgressDialog pd;
	String cityID;
	TextView point1, point2, point3, point4, point5;
	SearchShopAction searchShopAction;
	SearchShopModule searchShopModule;
	SearchShopPresistence searchShopPresistence;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		cityID = new SharePrefenceUtil(getActivity(), IConstants.SP_CITY).getCityID();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.search_shop, container, false);
		findView();
		setOnClick();

		return view;
	}

	@Override
	public void findView() {
		searchPoint = (ImageView) view.findViewById(R.id.search_point);
		point1 = (TextView) view.findViewById(R.id.point1);
		point2 = (TextView) view.findViewById(R.id.point2);
		point3 = (TextView) view.findViewById(R.id.point3);
		point4 = (TextView) view.findViewById(R.id.point4);
		point5 = (TextView) view.findViewById(R.id.point5);

		shut = (ImageView) view.findViewById(R.id.search_back);
		search_btn = (Button) view.findViewById(R.id.search);

		search = (EditText) view.findViewById(R.id.search_tv);

	}

	@Override
	public void setOnClick() {
		point1.setOnClickListener(this);
		point2.setOnClickListener(this);
		point3.setOnClickListener(this);
		point4.setOnClickListener(this);
		point5.setOnClickListener(this);
		shut.setOnClickListener(this);
		search_btn.setOnClickListener(this);

	}

	@Override
	public void showLoading() {
		dataLoadDialog.show();
		key = search.getText().toString();
		searchShopAction = new SearchShopAction(cityID, key, Distance);
		searchShopModule = new SearchShopModule();
		searchShopPresistence = new SearchShopPresistence(this);
		((ManagerActivity) getActivity()).addCurrentTask(searchShopPresistence);
		searchShopPresistence.setActitons(searchShopAction);
		searchShopPresistence.setModule(searchShopModule);
		searchShopPresistence.execute();
		dataLoadDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				if (searchShopPresistence != null) {
					searchShopPresistence.cancel(true);
				}
				getFragmentManager().popBackStack();

			}
		});
		dataLoadDialog.show();

	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			list = searchShopModule.list;
			if (list.size() != 0) {
				toSearchResultFragment();
			} else {
				Toast.makeText(getActivity(), "没有搜索结果", Toast.LENGTH_SHORT).show();
			}

		} else {
			showErrorTip(map);
		}
	}

	private void toSearchResultFragment() {
		((ShopActivity) getActivity()).setSearchShopList(list);
		SearchShopResultFragment searchResultFragment = new SearchShopResultFragment();
		getFragmentManager().beginTransaction().replace(R.id.fragment, searchResultFragment).addToBackStack(null)
				.commit();

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		
		
		switch (v.getId()) {
		case R.id.point1:
			Distance = "-1";
			searchPoint.setBackgroundResource(R.drawable.search_point1);
			break;
		case R.id.point2:
			Distance = "2000";
			searchPoint.setBackgroundResource(R.drawable.search_point2);
			break;
		case R.id.point3:
			Distance = "5000";
			searchPoint.setBackgroundResource(R.drawable.search_point3);
			break;
		case R.id.point4:
			Distance = "10000";
			searchPoint.setBackgroundResource(R.drawable.search_point4);
			break;
		case R.id.point5:
			Distance = "15000";
			searchPoint.setBackgroundResource(R.drawable.search_point5);
			break;
		case R.id.search_back:
			getFragmentManager().popBackStack();
			break;
		case R.id.search:
			showLoading();
			break;

		default:
			break;
		}
		

	}

}
